// 商品分类模块
import { topCategory } from '@/api/constants'
import {topCategoryAPI} from '@/api/category'

export default {
  namespaced: true,
  state() {
    return {
      // 分类信息集合，初始化
      list: topCategory.map(item => ({ name: item }))
    }
  },
  mutations: {
    setList(state,playload) {
      state.list = playload
    },
    show(state, id) {
      const currCategory = state.list.find(item => item.id === id)
      currCategory.open = true
      
    },
    hide(state, id) {
      const currCategory = state.list.find(item => item.id === id)
      currCategory.open = false
    }
  },
  actions: {
    // 获取数据
     async getList({ commit }) {
      const {result} = await topCategoryAPI()
      // 给每个分类加上控制二级分类显示隐藏的数据
      result.forEach(top => {
        top.open = false
      })
      commit('setList',result)
    }
  }
}
